/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.ui.format;

import info.xmlprint.core.format.Format;
import info.xmlprint.core.format.FormatGroup;
import info.xmlprint.font.FontManager;
import info.xmlprint.run.Constants;
import info.xmlprint.ui.message.GuiMessages;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

/**
 *  
 */
public class FontComposite extends GuiCommon implements IFormatSetup {
	private Composite comp;
	private Group groupFontSelection;
	private Combo comboFontShape;//
	private Spinner spinnerFontSize;//
	private Button buttonLineSpacingDouble;
	private Button buttonLineSpacingOneHalf;
	private Button buttonLineSpacingSingle;
	private Spinner spinnerLineSpacing;//
	private Combo comboLineSpacingUnit;//
	private Combo comboFontStretch;//
	private Combo comboLetterSpacing;//
	private Combo comboFontSizeUnit;
	private Combo comboFontFamily;//
	private Combo comboFontVariant;//
	private Combo comboTextDecoration;//
	private Combo comboTextColor;//
	private Combo comboPosition;//
	private Text textAddBefore, textAddAfter;

	private Button fontVariantInheritButton, fontVariantStandardButton;
	private Combo comboFontWeight;

	private Label schriftfamilieLabel;

	//
	private String[] itemListFontShapes_local, itemListFontWeight_local,
			itemListFontStretch_local, itemListFontVariant_local,
			itemListTextDecoration_local, itemListTextColor_local,
			itemListPosition_local, itemListLetterSpacing_local;

	private double factorLineSpacing = 0.0;

	private Format format, formatForSetup;

	private String standardReference;
	private boolean isFormatStandard;

	private String[] allFontNames;

	private FontManager fontManager;

	/**
	 * test
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Shell shell = new Shell(display);
		new FontComposite(shell, SWT.NULL, new Format());

		shell.setLayout(new FillLayout());
		shell.layout();

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public FontComposite(org.eclipse.swt.widgets.Composite parent, int style,
			Format f) {
		format = f;
		formatForSetup = f.objClone();
		comp = new Composite(parent, style);
		setFontNames();
		languageInit();

		initGUI();
	}

	private void setFontNames() {
		fontManager = FontManager.getInstance();
		Set<String> nameSet = fontManager.getRegisteredFamiliesSet();
		FormatGroup fg = format.getFormatGroup();
		if (fg != null) {
			nameSet.addAll(fg.getFontNameSet());
		}

		List<String> list = new ArrayList<String>(nameSet);
		Collections.sort(list, new Comparator<String>() {
			public int compare(String arg0, String arg1) {
				return arg0.compareToIgnoreCase(arg1);
			}
		});
		allFontNames = list.toArray(new String[list.size()]);
	}

	private void languageInit() {
		// font-style
		{
			String[] original = new String[] { "normal", "italic", "oblique" };

			itemListFontShapes_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_FontShape_up,
					GuiMessages.Stileditor_ParagraphFormat_FontShape_italic,
					GuiMessages.Stileditor_ParagraphFormat_FontShape_oblique };

			addInternationalMap("font-style", original,
					itemListFontShapes_local);

		}

		// font-weight
		{

			String[] original = new String[] { "100", "300", "400", "500",
					"700", "900" };

			itemListFontWeight_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_ultralight,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_light,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_normal,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_semibold,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_bold,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight_ultrabold };
			addInternationalMap("font-weight", original,
					itemListFontWeight_local);

		}

		// font-stretch
		{

			String[] original = new String[] { "ultra-condensed",
					"extra-condensed", "condensed", "semi-condensed", "normal",
					"semi-expanded", "expanded", "extra-expanded",
					"ultra-expanded" };

			itemListFontStretch_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_ultracondensed,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_extracondensed,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_condensed,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_semicondensed,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_normal,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_semiexpanded,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_expanded,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_extraexpanded,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch_ultraexpanded };

			addInternationalMap("font-stretch", original,
					itemListFontStretch_local);
		}

		// letter spacing
		{
			/*
			 * String[] original = new String[] { "", "2002", "2003", "2004",
			 * "2005", "2006", "2009", "200A" };
			 */
			String[] original = new String[] { "normal", "202F" };

			// TODO:
			/*
			 * itemListLetterSpacing_local = new String[] {
			 * GuiMessages.Stileditor_ParagraphFormat_LetterSpacing_none,
			 * "1 en", "1 em", "1/3 em", "1/4 em", "1/6 em", "Thin space",
			 * "Hair space" };
			 */
			itemListLetterSpacing_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_LetterSpacing_none,
					GuiMessages.Stileditor_ParagraphFormat_LetterSpacing_narrowNB };

			addInternationalMap("letter-spacing", original,
					itemListLetterSpacing_local);

		}

		// font-variant
		{
			String[] original = new String[] { "normal", "small-caps" };
			itemListFontVariant_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_FontVariant_normal,
					GuiMessages.Stileditor_ParagraphFormat_FontVariant_smallcaps };

			addInternationalMap("font-variant", original,
					itemListFontVariant_local);
		}

		// text-decoration
		{
			String[] original = new String[] { "none", "underline",
					"line-through", "overline" };

			itemListTextDecoration_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_Decoration_None,
					GuiMessages.Stileditor_ParagraphFormat_Decoration_Underline,
					GuiMessages.Stileditor_ParagraphFormat_Decoration_Linethrough,
					GuiMessages.Stileditor_ParagraphFormat_Decoration_Overline };

			addInternationalMap("text-decoration", original,
					itemListTextDecoration_local);
		}

		// color
		{
			String[] original = new String[] { "black", "aqua", "blue",
					"fuchsia", "gray", "green", "lime", "maroon", "navy",
					"olive", "purple", "red", "silver", "teal", "white",
					"yellow" };

			itemListTextColor_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_Color_Black,
					GuiMessages.Stileditor_ParagraphFormat_Color_Aqua,
					GuiMessages.Stileditor_ParagraphFormat_Color_Blue,
					GuiMessages.Stileditor_ParagraphFormat_Color_Fuchsia,
					GuiMessages.Stileditor_ParagraphFormat_Color_Gray,
					GuiMessages.Stileditor_ParagraphFormat_Color_Green,
					GuiMessages.Stileditor_ParagraphFormat_Color_Lime,
					GuiMessages.Stileditor_ParagraphFormat_Color_Maroon,
					GuiMessages.Stileditor_ParagraphFormat_Color_Navy,
					GuiMessages.Stileditor_ParagraphFormat_Color_Olive,
					GuiMessages.Stileditor_ParagraphFormat_Color_Purple,
					GuiMessages.Stileditor_ParagraphFormat_Color_Red,
					GuiMessages.Stileditor_ParagraphFormat_Color_Silver,
					GuiMessages.Stileditor_ParagraphFormat_Color_Teal,
					GuiMessages.Stileditor_ParagraphFormat_Color_White,
					GuiMessages.Stileditor_ParagraphFormat_Color_Yellow };

			addInternationalMap("color", original, itemListTextColor_local);
		}

		// position
		{
			String[] original = new String[] { "baseline", "super", "sub" };
			itemListPosition_local = new String[] {
					GuiMessages.Stileditor_ParagraphFormat_baseline_normal,
					GuiMessages.Stileditor_ParagraphFormat_baseline_super,
					GuiMessages.Stileditor_ParagraphFormat_baseline_sub };

			addInternationalMap("baseline-shift", original,
					itemListPosition_local);
		}
	}

	/*
	 * Einstellungswerte lesen und Format aktualisieren
	 */
	@Override
	public Format getFormat() {
		String s;

		// wenn Format standardwerte übernehmen soll, werde mit
		// standardReference
		// gespeichert.
		standardReference = "%@%";
		isFormatStandard = false;
		if (this.format.getType().equals("standard")) {
			standardReference = "";
			isFormatStandard = true;
		}

		String attrName = null;

		{
			attrName = "font-family";
			s = readCombo(comboFontFamily);
			setFormatNewValue(attrName, comboFontFamily, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { s =
			 * standardReference; }
			 * 
			 * if (!comboFontFamily.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		//
		{
			attrName = "font-style";
			s = readCombo(comboFontShape);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboFontShape, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { // Standard s =
			 * standardReference; }
			 * 
			 * if (!comboFontShape.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		//
		{
			attrName = "font-weight";
			s = readCombo(comboFontWeight);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboFontWeight, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { // Standard s =
			 * standardReference; }
			 * 
			 * if (!comboFontWeight.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		{
			attrName = "font-variant";
			s = readCombo(comboFontVariant);
			/*
			 * if (!fontVariantInheritButton.isEnabled()) { s = "Normal"; }
			 */
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboFontVariant, s);
		}

		//
		{
			attrName = "font-stretch";
			s = readCombo(comboFontStretch);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboFontStretch, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { s =
			 * standardReference; }
			 * 
			 * if (!comboFontStretch.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}
		{
			attrName = "letter-spacing";
			s = readCombo(comboLetterSpacing);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboLetterSpacing, s);
		}

		{
			attrName = "font-size";
			s = readSpinnerCombo(spinnerFontSize, comboFontSizeUnit);
			setFormatNewValue(attrName, spinnerFontSize, s);
			/*
			 * if (!isFormatStandard &&
			 * s.equals(this.format.getFormatGroup().getStandardFormat()
			 * .getAttribute(attrName))) { s = standardReference; }
			 * 
			 * if (!spinnerFontSize.isEnabled() || isFormatStandard) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		{
			// Line spacing
			attrName = "line-height";
			s = readSpinnerCombo(spinnerLineSpacing, comboLineSpacingUnit);
			setFormatNewValue(attrName, spinnerLineSpacing, s);
			/*
			 * if (!isFormatStandard &&
			 * s.equals(this.format.getFormatGroup().getStandardFormat()
			 * .getAttribute(attrName))) { s = standardReference; }
			 * 
			 * if (!spinnerLineSpacing.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute("line-height", s);
			 */

		}

		// text decoration
		{
			attrName = "text-decoration";
			s = readCombo(comboTextDecoration);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboTextDecoration, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { // Standard s =
			 * standardReference; }
			 * 
			 * if (!comboTextDecoration.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		{ // color
			attrName = "color";
			s = readCombo(comboTextColor);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboTextColor, s);
			/*
			 * if (!isFormatStandard && s.equals(this.format.getFormatGroup()
			 * .getStandardFormat().getAttribute(attrName))) { // Standard s =
			 * standardReference; }
			 * 
			 * if (!comboTextColor.isEnabled()) {
			 * this.format.setInherit(attrName, true); } else {
			 * this.format.setInherit(attrName, false); }
			 * 
			 * this.format.setAttribute(attrName, s);
			 */

		}

		{
			attrName = "text-addition-before";
			this.format.setAttribute(attrName, textAddBefore.getText());
			if (!isFormatStandard)
				textAddBefore.setEnabled(false);

		}

		{
			attrName = "text-addition-after";
			this.format.setAttribute(attrName, textAddAfter.getText());
			if (!isFormatStandard)
				textAddAfter.setEnabled(false);

		}

		// Position
		{
			attrName = "baseline-shift";
			s = readCombo(comboPosition);
			s = getOriginalValue(attrName, s);
			setFormatNewValue(attrName, comboPosition, s);

		}
		return this.format;

	}

	private void setFormatNewValue(String attrName, Control control,
			String controlValue) {
		if (!isFormatStandard
				&& controlValue.equals(this.format.getFormatGroup()
						.getStandardFormat().getAttribute(attrName))) {
			controlValue = standardReference;
		}

		if (!control.isEnabled()) {
			// if (!attrName.equals("font-variant")) //special handling for
			// small caps (does not solve the problem as "small-caps" is then
			// exported into the XSL-FO+ without being produced
			this.format.setInherit(attrName, true);
		} else {
			this.format.setInherit(attrName, false);
		}
		this.format.setAttribute(attrName, controlValue);
	}

	@Override
	public Composite getComp() {
		return comp;
	}

	private void initGUI() {
		comp.setLayout(new GridLayout(4, false));

		groupFontSelection = new Group(comp, SWT.NONE);

		groupFontSelection.setLayout(new GridLayout(5, false));
		groupFontSelection.setText(GuiMessages.Stileditor_Fonts_Settings);
		groupFontSelection.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 4, 0,
				0));
		{
			// Schriftfamilie
			schriftfamilieLabel = new Label(groupFontSelection, SWT.NONE);
			schriftfamilieLabel
					.setText(GuiMessages.Stileditor_ParagraphFormat_FontFamily
							+ ":");
			GridData schriftfamilieLabelGd = new GridData();
			schriftfamilieLabelGd.horizontalAlignment = GridData.END;
			schriftfamilieLabel.setLayoutData(schriftfamilieLabelGd);

			comboFontFamily = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontFamily.setLayoutData(getGD(0, 2, 150, 0));

			// TODO: limit to selected formats, e.g. OTF only

			if (allFontNames != null) {
				comboFontFamily.setItems(allFontNames);
			}
			if (format.getAttribute("font-family").isEmpty())
				comboFontFamily.select(0);
			// System.out.println("Wähle Schrift: " +
			// comboFontFamily.getText());

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			/*
			 * buttonFontOpentypeOnly = new Button(groupFontSelection, SWT.CHECK
			 * | SWT.LEFT); buttonFontOpentypeOnly.setLayoutData(new
			 * GridData()); buttonFontOpentypeOnly
			 * .setText(GuiMessages.Stileditor_Fonts_OnlyOpentype);
			 * buttonFontOpentypeOnly.setEnabled(false);
			 */

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-family", new Control[] { comboFontFamily });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);
			testFontFamily();

			comboFontFamily.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event event) {
					testFontFamily();
				}

			});

			// System.out.println("Initialisierung font-family abgeschlossen.");
		}
		{
			// schriftlage
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_FontShape + ":");

			comboFontShape = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontShape.setLayoutData(getGD(0, 2, 150, 0));
			comboFontShape.setItems(itemListFontShapes_local);
			// comboFontShape.select(0);

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			// addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-style", new Control[] { comboFontShape });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung font-style abgeschlossen.");
		}

		{
			// Schriftstaerke
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_FontWeight + ":");
			comboFontWeight = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontWeight.setLayoutData(getGD(0, 2, 150, 0));
			comboFontWeight.setItems(itemListFontWeight_local);
			// comboFontWeight.select(2);

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			// addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-weight", new Control[] { comboFontWeight });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung font-weight abgeschlossen.");

		}

		{
			// Laufweite
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_FontStretch + ":");
			comboFontStretch = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontStretch.setLayoutData(getGD(0, 2, 150, 0));
			comboFontStretch.setItems(itemListFontStretch_local);
			// comboFontStretch.select(0);

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			// addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-stretch", new Control[] { comboFontStretch });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung font-stretch abgeschlossen.");
		}

		{
			// letter-spacing
			// TODO:
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_LetterSpacing + ":");
			comboLetterSpacing = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboLetterSpacing.setLayoutData(getGD(0, 2, 150, 0));
			comboLetterSpacing.setItems(itemListLetterSpacing_local);
			// comboFontStretch.select(0);

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			// addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("letter-spacing", new Control[] { comboLetterSpacing });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung letter-spacing abgeschlossen.");
		}

		// Schriftvariante
		{
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_FontVariant + ":");
			comboFontVariant = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontVariant.setLayoutData(getGD(0, 2, 150, 0));
			comboFontVariant.setItems(itemListFontVariant_local); //
			// comboFontVariant.select(0);

			// add inherit Checkbox
			fontVariantInheritButton = new Button(groupFontSelection, SWT.CHECK);
			fontVariantInheritButton
					.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			fontVariantStandardButton = new Button(groupFontSelection, SWT.PUSH);
			fontVariantStandardButton
					.setText(GuiMessages.Stileditor_Gui_Standard);

			// // addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-variant", new Control[] { comboFontVariant });
			controlsInit(fontVariantStandardButton, fontVariantInheritButton,
					hm, null, formatForSetup);
		}

		{
			// Schriftgroesse
			addLabel(groupFontSelection,
					GuiMessages.Stileditor_ParagraphFormat_FontSize + ":");
			spinnerFontSize = new Spinner(groupFontSelection, SWT.NONE);
			spinnerFontSize.setLayoutData(getGD(0, 1, 50, 0));
			spinnerFontSize.setDigits(1);
			spinnerFontSize.setMinimum(0);
			spinnerFontSize.setMaximum(10000);
			spinnerFontSize.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					if (factorLineSpacing > 0) {
						spinnerLineSpacing.setSelection((int) Math
								.round(spinnerFontSize.getSelection()
										* factorLineSpacing));
					}
				}
			});

			comboFontSizeUnit = new Combo(groupFontSelection, SWT.READ_ONLY);
			comboFontSizeUnit.setLayoutData(getGD(0, 1, 70, 0));
			comboFontSizeUnit.setItems(itemListUnits);
			if (!this.format.getType().equals("standard")) // add relative font
															// size for
															// non-standard
															// formats
				comboFontSizeUnit.add("%");

			// add inherit Checkbox
			Button inheritButton = new Button(groupFontSelection, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(groupFontSelection, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			//
			// addLabel(groupFontSelection, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("font-size", new Control[] { spinnerFontSize,
					comboFontSizeUnit });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung font-size abgeschlossen.");

		}

		{
			Group otherGroup = new Group(comp, SWT.NONE);
			otherGroup.setLayout(new GridLayout(5, false));
			otherGroup.setText("");
			otherGroup.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 3, 0, 0));

			// Zeileabstand
			addLabel(otherGroup,
					GuiMessages.Stileditor_ParagraphFormat_LineSpacing + ":");

			spinnerLineSpacing = new Spinner(otherGroup, SWT.NONE);
			spinnerLineSpacing.setLayoutData(getGD(0, 1, 50, 0));
			spinnerLineSpacing.setDigits(1);
			spinnerLineSpacing.setIncrement(1);
			spinnerLineSpacing.setMaximum(10000);
			spinnerLineSpacing.setToolTipText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_Tooltip);
			spinnerLineSpacing.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					buttonLineSpacingSingle.setSelection(false);
					buttonLineSpacingOneHalf.setSelection(false);
					buttonLineSpacingDouble.setSelection(false);
				}
			});

			comboLineSpacingUnit = new Combo(otherGroup, SWT.READ_ONLY);
			comboLineSpacingUnit.setLayoutData(getGD(0, 1, 70, 0));
			comboLineSpacingUnit.add("");
			for (String s : itemListUnits) {
				comboLineSpacingUnit.add(s);
			}
			// comboLineSpacingUnit.select(0);

			// Falls ein Einheit gewählt wird, müssen die drei Schalter
			// ausgeschaltet werden
			comboLineSpacingUnit.addListener(SWT.Selection, new Listener() {
				@Override
				public void handleEvent(Event event) {
					if (comboLineSpacingUnit.getSelectionIndex() > 0) {
						buttonLineSpacingSingle.setSelection(false);
						buttonLineSpacingDouble.setSelection(false);
						buttonLineSpacingOneHalf.setSelection(false);
					}
				}
			});

			// add inherit Checkbox
			Button inheritButton = new Button(otherGroup, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(otherGroup, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);
			//
			// addLabel(otherGoup, "");

			// zeilig
			buttonLineSpacingSingle = new Button(otherGroup, SWT.CHECK
					| SWT.LEFT);
			buttonLineSpacingSingle.setLayoutData(new GridData());
			buttonLineSpacingSingle
					.setText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_One);
			buttonLineSpacingSingle
				.setToolTipText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_One_Tooltip);
			buttonLineSpacingSingle
					.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							buttonLineSpacingOneHalf.setSelection(false);
							buttonLineSpacingDouble.setSelection(false);
							if (buttonLineSpacingSingle.getSelection()) {
								spinnerLineSpacing.setSelection(10*Constants.lineheight/10);
								comboLineSpacingUnit.select(0); // keine
																// Einheit, da
																// Faktor
								factorLineSpacing = 0.0;
							} else {
								factorLineSpacing = 0.0;
							}

						}
					});

			buttonLineSpacingOneHalf = new Button(otherGroup, SWT.CHECK
					| SWT.LEFT);
			buttonLineSpacingOneHalf.setLayoutData(new GridData());
			buttonLineSpacingOneHalf
					.setText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_OneHalf);
			buttonLineSpacingOneHalf
			.setToolTipText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_OneHalf_Tooltip);
			buttonLineSpacingOneHalf
					.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							buttonLineSpacingSingle.setSelection(false);
							buttonLineSpacingDouble.setSelection(false);
							if (buttonLineSpacingOneHalf.getSelection()) {
								spinnerLineSpacing.setSelection(15*Constants.lineheight/10);
								comboLineSpacingUnit.select(0); // keine
																// Einheit, da
																// Faktor
								factorLineSpacing = 0.0;
							} else {
								factorLineSpacing = 0.0;
							}
						}
					});

			buttonLineSpacingDouble = new Button(otherGroup, SWT.CHECK
					| SWT.LEFT);
			buttonLineSpacingDouble.setLayoutData(new GridData());
			buttonLineSpacingDouble
					.setText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_Two);
			buttonLineSpacingDouble
				.setToolTipText(GuiMessages.Stileditor_ParagraphFormat_LineSpacing_Two_Tooltip);
			buttonLineSpacingDouble
					.addSelectionListener(new SelectionAdapter() {
						public void widgetSelected(SelectionEvent evt) {
							buttonLineSpacingOneHalf.setSelection(false);
							buttonLineSpacingSingle.setSelection(false);
							if (buttonLineSpacingDouble.getSelection()) {
								spinnerLineSpacing.setSelection(20*Constants.lineheight/10);
								comboLineSpacingUnit.select(0); // keine
																// Einheit, da
																// Faktor
								factorLineSpacing = 0.0;
							} else {
								factorLineSpacing = 0.0;
							}
						}
					});

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("line-height", new Control[] { spinnerLineSpacing,
					comboLineSpacingUnit });

			HashMap<String, Control> buttonMap = new HashMap<String, Control>();
			buttonMap.put("buttonLineSpacingSingle", buttonLineSpacingSingle);
			buttonMap.put("buttonLineSpacingOneAndAHalf",
					buttonLineSpacingOneHalf);
			buttonMap.put("buttonLineSpacingDouble", buttonLineSpacingDouble);

			controlsInit(standardButton, inheritButton, hm, buttonMap,
					formatForSetup);

			// System.out.println("Initialisierung line-spacing abgeschlossen.");

		}

		{
			Group decorationGroup = new Group(comp, SWT.NONE);
			decorationGroup.setLayout(new GridLayout(5, false));
			decorationGroup.setText("");
			decorationGroup.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 3, 0,
					0));
			// TextDecoration
			addLabel(decorationGroup,
					GuiMessages.Stileditor_ParagraphFormat_TextDecoration + ":");

			comboTextDecoration = new Combo(decorationGroup, SWT.READ_ONLY);
			comboTextDecoration.setLayoutData(getGD(0, 2, 150, 0));
			comboTextDecoration.setItems(itemListTextDecoration_local);
			// comboTextDecoration.select(0);

			// add inherit Checkbox
			Button inheritButton = new Button(decorationGroup, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(decorationGroup, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			// addLabel(decorationGroup, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("text-decoration", new Control[] { comboTextDecoration });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung text-decoration abgeschlossen.");
		}

		{
			// Farbe
			Group colorGroup = new Group(comp, SWT.NONE);
			colorGroup.setLayout(new GridLayout(5, false));
			colorGroup.setText("");
			colorGroup.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 4, 0, 0));
			// Schriftfarbe
			addLabel(colorGroup,
					GuiMessages.Stileditor_ParagraphFormat_TextColor + ":");

			comboTextColor = new Combo(colorGroup, SWT.NONE);
			comboTextColor.setLayoutData(getGD(0, 2, 150, 0));
			comboTextColor.setItems(itemListTextColor_local);
			// comboTextColor.select(0);

			// add inherit Checkbox
			Button inheritButton = new Button(colorGroup, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(colorGroup, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			// addLabel(colorGroup, "");

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("color", new Control[] { comboTextColor });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung text-color  abgeschlossen.");
		}

		{
			Group positionGroup = new Group(comp, SWT.NONE);
			positionGroup.setLayout(new GridLayout(5, false));
			positionGroup.setText("");
			positionGroup
					.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 4, 0, 0));
			// Position
			addLabel(positionGroup,
					GuiMessages.Stileditor_ParagraphFormat_TextPosition + ":");

			comboPosition = new Combo(positionGroup, SWT.READ_ONLY);
			comboPosition.setLayoutData(getGD(0, 2, 150, 0));
			comboPosition.setItems(itemListPosition_local);

			// add inherit Checkbox
			Button inheritButton = new Button(positionGroup, SWT.CHECK);
			inheritButton.setText(GuiMessages.Stileditor_Gui_Inherit);

			// add Standard Checkbox
			Button standardButton = new Button(positionGroup, SWT.PUSH);
			standardButton.setText(GuiMessages.Stileditor_Gui_Standard);

			// set Standard Checkbox and controlsvalue
			HashMap<String, Control[]> hm = new HashMap<String, Control[]>();
			hm.put("baseline-shift", new Control[] { comboPosition });
			controlsInit(standardButton, inheritButton, hm, null,
					formatForSetup);

			// System.out.println("Initialisierung baseline-shift abgeschlossen.");
		}

		{
			Group group = new Group(comp, SWT.NONE);
			group.setLayout(new GridLayout(2, false));//TODO: parent to large
			group.setText("");
			group.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 2, 0, 0));
			// Textaddition before
			addLabel(group,
					GuiMessages.Stileditor_ParagraphFormat_TextAddition_Before
							+ ":");
			textAddBefore = new Text(group, SWT.SINGLE | SWT.BORDER);
			GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
			textAddBefore.setLayoutData(gridData);
			textAddBefore.setText(format.getAttribute("text-addition-before"));
			textAddBefore
					.setToolTipText(GuiMessages.Stileditor_Tooltip_Textaddition);

			// group.setLayoutData(getGD(GridData.FILL_HORIZONTAL, 4, 0, 0));
			addLabel(group,
					GuiMessages.Stileditor_ParagraphFormat_TextAddition_After
							+ ":");
			textAddAfter = new Text(group, SWT.SINGLE | SWT.BORDER);
			GridData gridData2 = new GridData(GridData.FILL_HORIZONTAL);
			textAddAfter.setLayoutData(gridData2);
			textAddAfter.setText(format.getAttribute("text-addition-after"));
			textAddAfter
					.setToolTipText(GuiMessages.Stileditor_Tooltip_Textaddition);
		}
		// System.out.println("Initialisierung text-after abgeschlossen.");
	}

	@Override
	public boolean controlValueVertify() {
		// TODO Auto-generated method stub
		return true;
	}

	private void testFontFamily() {
		String name = comboFontFamily.getItem(comboFontFamily
				.getSelectionIndex());
		if (!fontManager.isLocalFont(name)) {
			schriftfamilieLabel.setForeground(Display.getDefault()
					.getSystemColor(SWT.COLOR_RED));
			schriftfamilieLabel
					.setToolTipText(GuiMessages.Stileditor_Info_Font_Missing_3
							+ " " + name
							+ GuiMessages.Stileditor_Info_Font_Missing_5);
		} else {
			schriftfamilieLabel.setForeground(Display.getDefault()
					.getSystemColor(SWT.COLOR_BLACK));
			schriftfamilieLabel.setToolTipText("");
		}
	}

}
